<template>
  <div>
    <el-form
      ref="query"
      :model="query"
      :inline="true"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="借阅者">
        <el-input v-model="query.username" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="图书编号">
        <el-input v-model="query.book_num" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="query.book_name" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="query.writer" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="所在位置">
        <el-input v-model="query.site" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="借阅时间">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="query.lend_time"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="归还时间">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="query.revert_time"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否归还">
        <el-select v-model="query.state">
          <el-option
            v-for="item in state"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-button
        @click="bookQuery"
        size="mini"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button @click="resetForm" size="mini" icon="el-icon-refresh"
        >重置</el-button
      >
    </el-form>

    <el-table :data="tableData" border size="mini">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="username" label="借阅者" width="100">
      </el-table-column>
      <el-table-column prop="book_num" label="图书编号" width="180">
      </el-table-column>
      <el-table-column prop="book.book_name" label="图书名称" width="140">
      </el-table-column>
      <el-table-column prop="book.writer" label="作者" width="90">
      </el-table-column>
      <el-table-column prop="book.site" label="位置" width="90">
      </el-table-column>
      <el-table-column prop="lend_time" label="借阅时间" width="150">
      </el-table-column>
      <el-table-column prop="revert_time" label="预计归还时间" width="100">
      </el-table-column>
      <el-table-column prop="real_time" label="归还时间" width="150">
      </el-table-column>
      <el-table-column prop="state" label="是否归还" width="95">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.state == 1">已归还</el-tag>
          <el-tag type="danger" v-if="scope.row.state == 0">未归还</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="remindUser(scope.$index, scope.row)"
            >提醒用户归还</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDataTotal"
      ></el-pagination>
    </div>

    <el-dialog title="提醒用户归还" v-model="messageDialogShow" width="30%">
      <el-form
        ref="messageForm"
        :model="messageForm"
        :inline="true"
        label-width="80px"
        size="mini"
      >
        <el-form-item>
          <el-input
            v-model="messageForm.content"
            placeholder="请输入所要发送的消息内容"
          ></el-input>
        </el-form-item>
        <div style="text-align: right">
          <el-button size="mini" @click="messageDialogShow = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="messageSubmit"
            >确 定</el-button
          >
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { page } from "../../api/book/borrow";
import { create } from "../../api/message/message";
export default {
  data() {
    return {
      tableData: [],
      query: { page: 1, limit: 10 },
      tableDataTotal: 0,
      state: ["已归还", "未归还"],
      messageDialogShow: false,
      messageForm: {},
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    indexMethod(index) {
      return (this.query.page - 1) * this.query.limit + (index + 1);
    },
    getPage() {
      page(this.query).then((res) => {
        // console.log(res);
        this.tableData = res.data.rows;
        this.tableDataTotal = res.data.count;
      });
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.getPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getPage();
    },
    bookQuery() {
      this.getPage();
    },
    resetForm() {
      this.query = { page: 1, limit: 10 };
      this.getPage();
    },
    remindUser(index, row) {
      if (row.state == "1") {
        this.$message.warning(
          `这本【${row.book.book_name}】用户【${row.username}】已经归还了，无法提醒`
        );
      } else {
        this.messageDialogShow = true;
        this.messageForm = { ...row };
      }
    },
    messageSubmit() {
      create({
        username: this.messageForm.username,
        book_name: this.messageForm.book.book_name,
        book_num: this.messageForm.book_num,
        revert_time: this.messageForm.revert_time,
        content: this.messageForm.content,
      })
        .then((res) => {
          if (res.success) {
            this.$message({ message: "提醒用户成功！", type: "success" });
            this.messageDialogShow = false;
            // this.getPage();
          } else {
            this.$message.error(res.data);
          }
        })
        .catch(() => this.$message.error("提醒用户失败！"));
    },
  },
};
</script>
<style lang="scss" scoped>
</style>